package com.mzj.saas.rent;

import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.HouseInspection;
import com.haixiaoke.saas.repository.service.ICommunityHouseUserService;
import com.haixiaoke.saas.repository.service.IHouseInspectionService;
import com.haixiaoke.saas.repository.vo.HouseInspectionVo;
import com.mzj.saas.annotation.CheckAuth;
import com.mzj.saas.annotation.RepeatSubmit;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.commons.StatusCode;
import com.mzj.saas.mservice.propertybill.service.WithDrawService;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 验房信息Controller
 *
 * @author haixiaoke
 * @date 2023-04-18
 */
@RestController
@RequestMapping("/house/inspection")
public class HouseInspectionController extends BaseController {
    @Autowired
    private IHouseInspectionService houseInspectionService;
    @Autowired
    private LoginUserUtil loginUserUtil;
    @Autowired
    private WithDrawService withDrawService;
    @Autowired
    private ICommunityHouseUserService iCommunityHouseUserService;
    /**
     * 查询验房信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseInspection houseInspection) {
        startPage();
        List<HouseInspection> list = houseInspectionService.selectHouseInspectionList(houseInspection);
        return getDataTable(list);
    }

    /**
     * 查询验房信息
     */
    @GetMapping("/info")
    @CheckAuth
    public AjaxResult info(HouseInspection houseInspection) {
        HouseInspectionVo houseInspectionVo = houseInspectionService.selectHouseInspection(houseInspection);
        return success(houseInspectionVo);
    }
    /**
     * 查询验房单
     */
    @GetMapping("/unit")
    @CheckAuth
    public AjaxResult unit(HouseInspection houseInspection) {
        Integer loginType = loginUserUtil.getLoginType();
        HouseInspectionVo houseInspectionVo = houseInspectionService.selectHouseInspectionCleaning(houseInspection,loginType);
        return success(houseInspectionVo);
    }

    @PostMapping("unit/{orderId}")
    public AjaxResult unit(@PathVariable("orderId")String orderId){
        HouseInspectionVo houseInspectionVo = houseInspectionService.selectHouseInspectionListByOrderId(orderId);
        return success(houseInspectionVo);
    }

    /**
     * 获取验房信息详细信息
     */
    @GetMapping(value = "/{inspectionId}")
    public AjaxResult getInfo(@PathVariable("inspectionId") Long inspectionId) {
        return success(houseInspectionService.selectHouseInspectionByInspectionId(inspectionId));
    }

    /**
     * 新增用户入住验房信息
     */
    @PostMapping("/inspectionOnCheckIn")
    @CheckAuth
    @Transactional
    @RepeatSubmit
    public AjaxResult inspectionOnCheckIn (@RequestBody HouseInspectionVo houseInspectionVo) {
        String id = loginUserUtil.getId();
        return toAjax(houseInspectionService.inspectionOnCheckIn (houseInspectionVo,id));
    }

    /**
     * 新增用户退租验房信息
     */
    @PostMapping("/userInspectionOnCheckOut")
    @CheckAuth
    @Transactional
    @RepeatSubmit
    public AjaxResult userInspectionOnCheckOut(@RequestBody HouseInspectionVo houseInspectionVo) {
        String id = loginUserUtil.getId();

        //退租用户验房，需要上传对应房间的图片;
        String houseImg = houseInspectionVo.getHouseImg();
        if (houseImg.isEmpty()) {
            return error("请上传照片");
        }
        int flag = houseInspectionService.userInspectionOnCheckOut(houseInspectionVo, id);

        return toAjax(flag);
    }

    /**
     * 新增企业退租验房信息
     */
    @PostMapping("/enterpriseInspectionOnCheckOut")
    @CheckAuth
    @Transactional
    @RepeatSubmit
    public AjaxResult enterpriseInspectionOnCheckOut(@RequestBody HouseInspectionVo houseInspectionVo) {
        String id = loginUserUtil.getId();

        int flag = houseInspectionService.enterpriseInspectionOnCheckOut(houseInspectionVo, id);
        if (flag == 0){
            return AjaxResult.error("商家退租失败");
        }

        String token = loginUserUtil.getToken();
        String houseId = houseInspectionVo.getHouseId();
        //用户验房后将授权删除
        ResultBean<Object> resultBean = withDrawService.deleteAuth(token, houseId);
        String code = resultBean.getCode();
        if (!StatusCode.SUCCESS_CODE_10000.getErrorCode().equals(code)){
            return AjaxResult.error();
        }

        //删除成员列表
        iCommunityHouseUserService.updateCommunityHouseUserStatus(houseId);

        //todo
        //商家验房之后新增退租账单

        return AjaxResult.success();
    }
    /**
     * 修改验房信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody HouseInspection houseInspection) {
        return toAjax(houseInspectionService.updateHouseInspection(houseInspection));
    }

    /**
     * 删除验房信息
     */
	@DeleteMapping("/{inspectionIds}")
    public AjaxResult remove(@PathVariable Long[] inspectionIds) {
        return toAjax(houseInspectionService.deleteHouseInspectionByInspectionIds(inspectionIds));
    }
}
